﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.FallsDropBed
{
    public partial class FallsDropNursingHistory : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 窗体构造函数
        public FallsDropNursingHistory(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }
        #endregion

        #region GridView设置
        private void BindControl()
        {
            StatDate.DateTime = DateTime.Now.AddDays(-30).Date;
            EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (PatientInfo == null) return;
            string patientid = PatientInfo.PATIENT_ID;//住院号
            int VisitId = PatientInfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.FallDown;
            DataTable dt = new DataTable();
            dt.Columns.Add("NianLin1", typeof(string));
            dt.Columns.Add("NianLin2", typeof(string));
            dt.Columns.Add("JiWangShi1", typeof(string));
            dt.Columns.Add("JiWangShi2", typeof(string));
            dt.Columns.Add("HuoDongNengLi1", typeof(string));
            dt.Columns.Add("HuoDongNengLi2", typeof(string));
            dt.Columns.Add("YiShiZhuangTai1", typeof(string));
            dt.Columns.Add("GanJueZhangAi1", typeof(string));
            dt.Columns.Add("YongYaoQingKuang1", typeof(string));
            dt.Columns.Add("YongYaoQingKuang2", typeof(string));
            dt.Columns.Add("PaiXieWenTi1", typeof(string));
            dt.Columns.Add("ZhiLiaoYinSu1", typeof(string));
            dt.Columns.Add("HuLiCuoShi1", typeof(string));
            dt.Columns.Add("HuLiCuoShi2", typeof(string));
            dt.Columns.Add("HuLiCuoShi3", typeof(string));
            dt.Columns.Add("HuLiCuoShi4", typeof(string));
            dt.Columns.Add("HuLiCuoShi5", typeof(string));
            dt.Columns.Add("HuLiCuoShi6", typeof(string));
            dt.Columns.Add("HuLiCuoShi7", typeof(string));
            dt.Columns.Add("HuLiCuoShi8", typeof(string));
            dt.Columns.Add("HuLiCuoShi9", typeof(string));
            dt.Columns.Add("HuLiCuoShi10", typeof(string));
            dt.Columns.Add("HuLiCuoShi11", typeof(string));
            dt.Columns.Add("HuLiCuoShi12", typeof(string));
            dt.Columns.Add("ZongFen", typeof(string));
            dt.Columns.Add("ZeRenHuShiQingZi", typeof(string));
            dt.Columns.Add("HuShiZhangQianZi", typeof(string));
            dt.Columns.Add("HuanZheJiaShuQianZi", typeof(string));
            dt.Columns.Add("YuHuanZheGuanXi", typeof(string));
            dt.Columns.Add("YuFangXiaoGuo1", typeof(string));
            dt.Columns.Add("YuFangXiaoGuo2", typeof(string));
            dt.Columns.Add("YuFangXiaoGuo_Text", typeof(string));
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ChuYuanRiQi", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    FallDown fd = new FallDown();
                    fd = XmlUtil.XMLToObject<FallDown>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    //年龄
                    string NianLin = fd.PingGuXiangMu.NianLin;
                    int[] nianlinArray = { 2, 1 };
                    if (string.IsNullOrEmpty(NianLin)) NianLin = "0#0";
                    if (NianLin.Equals("0")) NianLin = "1#0";
                    if (NianLin.Equals("1")) NianLin = "0#1";
                    if (NianLin.IndexOf('#') < 0) NianLin = "0#0";
                    if (NianLin.Split('#').Length < 2) NianLin = "0#0";
                    if (NianLin.Split('#')[0].Equals("1")) newRow["NianLin1"] = 2;
                    if (NianLin.Split('#')[1].Equals("1")) newRow["NianLin2"] = 10;
                    //既往史
                    string JiWangShi = fd.PingGuXiangMu.JiWangShi;
                    string[] JiArray;
                    if (!string.IsNullOrEmpty(JiWangShi))
                    {
                        JiArray = JiWangShi.Split('#');
                        if (Convert.ToInt32(JiArray[0]) == 1)
                        {
                            newRow["JiWangShi1"] = 2;
                        }
                        if (Convert.ToInt32(JiArray[1]) == 1)
                        {
                            newRow["JiWangShi2"] = 1;
                        }
                    }
                    //活动能力
                    string HuoDongNengLi = fd.PingGuXiangMu.HuoDongNengLi;
                    string[] HuoArray;
                    if (!string.IsNullOrEmpty(HuoDongNengLi))
                    {
                        HuoArray = HuoDongNengLi.Split('#');
                        if (Convert.ToInt32(HuoArray[0]) == 1)
                        {
                            newRow["HuoDongNengLi1"] = "3";
                        }
                        if (Convert.ToInt32(HuoArray[1]) == 1)
                        {
                            newRow["HuoDongNengLi2"] = "2";
                        }
                    }
                    //意识状态
                    string YiShiZhuangTai = fd.PingGuXiangMu.YiShiZhuangTai;
                    if (!string.IsNullOrEmpty(YiShiZhuangTai))
                    {
                        int n = Convert.ToInt32(YiShiZhuangTai);
                        newRow["YiShiZhuangTai1"] = n == 0 ? "2" : "";
                    }
                    //感觉障碍
                    string GanJueZhangAi = fd.PingGuXiangMu.GanJueZhangAi;
                    if (!string.IsNullOrEmpty(GanJueZhangAi))
                    {
                        int n = Convert.ToInt32(GanJueZhangAi);
                        newRow["GanJueZhangAi1"] = n == 0 ? "2" : "";
                    }
                    //用药情况
                    string YongYaoQingKuang = fd.PingGuXiangMu.YongYaoQingKuang;
                    if (!string.IsNullOrEmpty(YongYaoQingKuang))
                    {
                        if (YongYaoQingKuang.Contains("#"))
                        {
                            string[] YongArray = YongYaoQingKuang.Split('#');
                            if (Convert.ToInt32(YongArray[0]) == 1)
                            {
                                newRow["YongYaoQingKuang1"] = "1";
                            }
                            YongArray = YongYaoQingKuang.Split('#');
                            if (Convert.ToInt32(YongArray[1]) == 1)
                            {
                                newRow["YongYaoQingKuang2"] = "2";
                            }
                        }
                        else
                        {
                            if (YongYaoQingKuang == "0")
                            {
                                newRow["YongYaoQingKuang1"] = "1";
                            }
                            if (YongYaoQingKuang == "1")
                            {
                                newRow["YongYaoQingKuang2"] = "2";
                            }
                        }
                    }
                    //排泄问题
                    string PaiXieWenTi = fd.PingGuXiangMu.PaiXieWenTi;
                    if (!string.IsNullOrEmpty(PaiXieWenTi))
                    {
                        int n = Convert.ToInt32(PaiXieWenTi);
                        newRow["PaiXieWenTi1"] = n == 0 ? "2" : "";
                    }
                    //治疗因素
                    string ZhiLiaoYinSu = fd.PingGuXiangMu.ZhiLiaoYinSu;
                    if (!string.IsNullOrEmpty(ZhiLiaoYinSu))
                    {
                        int n = Convert.ToInt32(ZhiLiaoYinSu);
                        newRow["ZhiLiaoYinSu1"] = n == 0 ? "2" : "";
                    }
                    //护理措施
                    string HuLiCuoShi = fd.PingGuXiangMu.HuLiCuoShi;
                    string[] HuArray;
                    if (!string.IsNullOrEmpty(HuLiCuoShi))
                    {
                        HuArray = HuLiCuoShi.Split('#');
                        if (Convert.ToInt32(HuArray[0]) == 1)
                        {
                            newRow["HuLiCuoShi1"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[1]) == 1)
                        {
                            newRow["HuLiCuoShi2"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[2]) == 1)
                        {
                            newRow["HuLiCuoShi3"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[3]) == 1)
                        {
                            newRow["HuLiCuoShi4"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[4]) == 1)
                        {
                            newRow["HuLiCuoShi5"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[5]) == 1)
                        {
                            newRow["HuLiCuoShi6"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[6]) == 1)
                        {
                            newRow["HuLiCuoShi7"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[7]) == 1)
                        {
                            newRow["HuLiCuoShi8"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[8]) == 1)
                        {
                            newRow["HuLiCuoShi9"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[9]) == 1)
                        {
                            newRow["HuLiCuoShi10"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[10]) == 1)
                        {
                            newRow["HuLiCuoShi11"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[11]) == 1)
                        {
                            newRow["HuLiCuoShi12"] = "√";
                        }
                    }
                    //总分
                    newRow["ZongFen"] = fd.PingGuXiangMu.ZongFen;
                    //责任护士签名
                    newRow["ZeRenHuShiQingZi"] = fd.PingGuXiangMu.ZeRenHuShiQingZi;
                    //护士长签名
                    newRow["HuShiZhangQianZi"] = fd.PingGuXiangMu.HuShiZhangQianZi;
                    //患者家属签字
                    newRow["HuanZheJiaShuQianZi"] = fd.HuanZheJiaShuQianZi;
                    //与患者关系
                    newRow["YuHuanZheGuanXi"] = fd.YuHuanZheGuanXi;
                    //预防效果
                    string yufangxiaoguo = fd.YuFangXiaoGuo;
                    if (!string.IsNullOrEmpty(yufangxiaoguo))
                    {
                        if (yufangxiaoguo.Contains("#"))
                        {
                            string[] yufangArray = yufangxiaoguo.Split('#');
                            if (Convert.ToInt32(yufangArray[0]) == 1)
                            {
                                newRow["YuFangXiaoGuo1"] = "√";
                            }
                            if (Convert.ToInt32(yufangArray[1]) == 1)
                            {
                                newRow["YuFangXiaoGuo2"] = "√";
                            }
                        }
                        else
                        {
                            if (yufangxiaoguo == "0")
                            {
                                newRow["YuFangXiaoGuo1"] = "√";
                            }
                            else
                            {
                                newRow["YuFangXiaoGuo2"] = "√";
                            }
                        }
                    }
                    //预防效果文本框
                    newRow["YuFangXiaoGuo_Text"] = fd.YuFangXiaoGuo_Text;
                    //日期
                    string RiQi = fd.PingGuXiangMu.RiQi;
                    DateTime date = Convert.ToDateTime(RiQi);
                    newRow["RiQi"] = date.ToString("yyyy-MM-dd");
                    //出院日期
                    string ChuYuanRiQi = fd.ChuYuanRiQi;
                    if (!string.IsNullOrEmpty(ChuYuanRiQi))
                    {
                        DateTime dte = Convert.ToDateTime(ChuYuanRiQi);
                        newRow["ChuYuanRiQi"] = dte.ToString("yyyy-MM-dd");
                    }
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region 查询
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (bandedGridView1.DataRowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddFallsDropNursing add = new AddFallsDropNursing(LoginUser, PatientInfo, Id);
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region  新增
        private void btnxinzeng_Click(object sender, EventArgs e)
        {
            AddFallsDropNursing addForm = new AddFallsDropNursing(LoginUser, PatientInfo, "");
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region   修改
        private void Btnxiugai_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddFallsDropNursing addForm = new AddFallsDropNursing(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region  打印
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            FallsDropPrint f = new FallsDropPrint(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }
        #endregion

        #region  删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = StatDate.Text;
                    string dateEnd = EndDate.Text;
                    BindInfo();
                }
            }
        }
        #endregion
    }
}